<ui:composition template="template.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets">

	<ui:define name="head">
		<script type="text/javascript">
			function confirmUpload1(fileType) {
				// if (confirm('是否确定上传(覆盖)该 '
				// 		+ fileType
				// 		+ ' 文件?\n上传损坏的文件可能会导致系统失效甚至崩溃.')) {
				// 	return true;
				// }
				var input1 = $("#fileInput1").val();
				if(""=== input1 || input1.isEmpty){
					return false;
				}
				return true;
			}
			function confirmUpload2(fileType) {
				var input2 = $("#fileInput2").val();
				if(""=== input2 || input2.isEmpty){
					return false;
				}
				return true;
			}
			function confirmUpload3(fileType) {
				var input3 = $("#fileInput3").val();
				if(""=== input3 || input3.isEmpty){
					return false;
				}
				return true;
			}
			$(function(){
				$('#okButton1').click(function(){
					$.ajax({
						type: "POST",
						url: "repository/"+$("#usertenant").val()+"/conf.xml",
						data: {},
						dataType: "json",
						success: function(data){
							$("#shangchuan1").modal("hide");
							window.location.reload();
						}
					});
				});
			});

			function showUploadJobForm(button) {
				var contentClone = $("#UploadFileTemplateDiv").clone();
				applyFileSelectEventHandler(contentClone);
				$(button).replaceWith(contentClone);
			}
		</script>
		<link rel="stylesheet" type="text/css"
			href="#{request.contextPath}/stylesheets/repository.css" />
	</ui:define>

	<ui:define name="content">
		<div id="RepositoryPage" class="container">
			<h:panelGroup
				rendered="#{param['configuration_upload'] == 'Success'}">
				<div class="alert alert-success">
					配置文件 <a href="repository/#{user.tenant}/conf.xml"
						target="_blank">conf.xml</a> 上传成功.
				</div>
			</h:panelGroup>

			<h:panelGroup
				rendered="#{param['job_upload'] == 'Success'}">
				<div class="alert alert-success">
					任务文件 <b>#{param['job_filename']}</b> 已成功更新.
				</div>
			</h:panelGroup>

			<h:panelGroup rendered="#{user.configurationEditor and monitorUIPreferences.showConfXmlUploadOptions}">
				<div class="panel panel-default panel-right">
					<div class="panel-heading">配置文件</div>
					<div class="panel-body">
						<p>
							上传新的 <a href="repository/#{user.tenant}/conf.xml"
								target="_blank">conf.xml</a> 文件:
						</p>
						<input id="usertenant" type="hidden" value="#{user.tenant}" />
						<div class="modal fade bs-example-modal-lg" id="shangchuan1" tabindex="-1" role="dialog"
							 aria-labelledby="myModalLabel">
							<div class="modal-dialog" role="document">
								<div class="modal-content">
									<div class="modal-header">
										<h4 id="h4Id" class="modal-title" align="center" style="font-size: 20px;color: #16c2f4"></h4>
									</div>
									<div class="modal-footer">
										<button id="okButton1" type="button" class="btn btn-primary" >确定</button>
										<button id="noButton" type="button" class="btn btn-default" data-dismiss="modal">取消</button>
									</div>
								</div>
							</div>
						</div>
						<form action="repository/#{user.tenant}/conf.xml"
							method="post" enctype="multipart/form-data">

							<div class="input-group">
								<span class="input-group-btn">
									<span class="btn btn-primary btn-sm btn-file">
										浏览 … <input type="file" name="file" />
									</span>
								</span>
								<input id="fileInput1" type="text" class="form-control input-sm" readonly="readonly" />
							</div>

							<div class="spacer-xs"></div>

							<button type="submit"
								class="btn btn-warning btn-block"
								onclick="return confirmUpload1('configuration')">
								<span class="glyphicon glyphicon-upload" aria-hidden="true" />
								<span>上传</span>
							</button>
						</form>
					</div>
				</div>
			</h:panelGroup>

			<h:panelGroup rendered="#{user.admin and monitorUIPreferences.showRepositoryUploadOptions}">
				<div class="panel panel-default panel-right">
					<div class="panel-heading">存储库备份+恢复</div>
					<div class="panel-body">
						<p>
							你能下载 <a href="repository/#{user.tenant}/zip">这个zip格式的存储库压缩包</a>,将其用作备份或复制到另一个地方的方法。
						</p>
						<p>通过相应的zip存储库上传文件:</p>
						<div class="spacer-sm"></div>

						<form action="repository/#{user.tenant}/zip" method="post"
							enctype="multipart/form-data">

							<div class="input-group">
								<span class="input-group-btn">
									<span class="btn btn-primary btn-file btn-sm">
										浏览 … <input type="file" name="file" />
									</span>
								</span>
								<input id="fileInput2" type="text" class="form-control input-sm" readonly="readonly" />
							</div>

							<div class="spacer-xs"></div>

							<button type="submit"
								class="btn btn-warning btn-block"
								onclick="return confirmUpload2('repository')">
								<span class="glyphicon glyphicon-upload" aria-hidden="true" />
								<span>上传</span>
							</button>
						</form>
					</div>
				</div>
			</h:panelGroup>

			<h2>任务</h2>
			<ul class="repositoryView">
				<ui:repeat var="folder" value="#{tenantContext.jobFolder.folders}">
					<li class="folder">#{folder.name}</li>
					<ul>
						<ui:repeat var="file" value="#{folder.files}">
							<li class="file #{file.type}">#{file.name}</li>
						</ui:repeat>
					</ul>
				</ui:repeat>
				<ui:repeat var="file" value="#{tenantContext.jobFolder.files}">
					<h:panelGroup rendered="#{file.type eq 'ANALYSIS_JOB'}">
						<li class="file #{file.type}"><a href="repository/#{user.tenant}/jobs/#{file.name}" target="_blank">#{file.name}</a>
							<h:panelGroup rendered="#{user.jobEditor}"
								styleClass="uploadJobPanel">
								<form action="repository/#{user.tenant}/jobs/#{file.name}"
									method="post" enctype="multipart/form-data" class="form-inline" style="display: inline;">
										<button onclick="showUploadJobForm(this);" class="text-warning btn-textlike" type="button">
											<span class="glyphicon glyphicon-upload" aria-hidden="true" />
										</button>
								</form>
							</h:panelGroup></li>
					</h:panelGroup>
					<h:panelGroup rendered="#{file.type ne 'ANALYSIS_JOB'}">
						<li class="file #{file.type}">#{file.name}</li>
					</h:panelGroup>
				</ui:repeat>
			</ul>

			<h2>时间表</h2>
			<ul class="repositoryView">
				<ui:repeat var="folder" value="#{tenantContext.timelineFolder.folders}">
					<li class="folder">#{folder.name}</li>
					<ul>
						<ui:repeat var="file" value="#{folder.files}">
							<li class="file #{file.type}"><a href="dashboard##{folder.name}" target="_blank">#{file.name}</a></li>
						</ui:repeat>
					</ul>
				</ui:repeat>
				<ui:repeat var="file" value="#{tenantContext.timelineFolder.files}">
					<li class="file #{file.type}"><a href="dashboard" target="_blank">#{file.name}</a></li>
				</ui:repeat>
			</ul>

			<h2>数据存储</h2>
			<ul class="repositoryView">
				<ui:repeat var="ds" value="#{jsfHelper.datastores}">
					<li class="file DATASTORE">#{ds.name}</li>
				</ui:repeat>
			</ul>

			<h2>结果报告</h2>
			<ul class="repositoryView">
				<ui:repeat var="folder" value="#{tenantContext.resultFolder.folders}">
					<li class="folder">#{folder.name}</li>
					<ul>
						<ui:repeat var="file" value="#{folder.files}">
							<li class="file #{file.type}">#{file.name}</li>
						</ui:repeat>
					</ul>
				</ui:repeat>
				<ui:repeat var="file" value="#{tenantContext.resultFolder.files}">
					<h:panelGroup rendered="#{file.type eq 'ANALYSIS_RESULT'}">
						<li class="file #{file.type}"><a href="repository/#{user.tenant}/results/#{file.name}" target="_blank">#{file.name}</a></li>
					</h:panelGroup>
					<h:panelGroup rendered="#{file.type ne 'ANALYSIS_RESULT'}">
						<li class="file #{file.type}">#{file.name}</li>
					</h:panelGroup>
				</ui:repeat>
			</ul>
		</div>

		<div style="visibility: hidden; display: none">
			<div id="UploadFileTemplateDiv">
				<p>替换任务文件:</p>
				<div class="input-group">
					<span class="input-group-btn"> <span
						class="btn btn-primary btn-file btn-sm"> 浏览 … <input
							type="file" name="file" />
					</span>
					</span> <input id="fileInput3" type="text" class="form-control input-sm" readonly="readonly" />
				</div>

				<button type="submit" class="btn btn-primary btn-sm"
					onclick="return confirmUpload3('job')">
					<span class="glyphicon glyphicon-upload" aria-hidden="true" /> <span>上传</span>
				</button>

			</div>
		</div>
	</ui:define>

</ui:composition>


